﻿namespace WMS6.Web
{
    using System;
    using System.IO;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web.LoginServiceRef;

    public class Portal : BasePage
    {
        protected Button btnLogin;
        protected HtmlForm form1;
        protected HtmlImage imgLogin;
        protected Label lblMsg;
        protected Label lblPassword;
        protected Label lblUserName;
        protected HtmlInputPassword txtPassword;
        protected TextBox txtUserName;

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
                {
                    this.lblMsg.Text = "请输入用户名!";
                }
                else if (string.IsNullOrEmpty(this.txtPassword.Value.Trim()))
                {
                    this.lblMsg.Text = "请输入密码!";
                }
                else
                {
                    string userName = this.txtUserName.Text.Trim();
                    string userPwd = this.txtPassword.Value.Trim();
                    UserMstInfo userInfo = LoginServiceClientProxy.GetInstance().GetUserInfoByID(userName);
                    if (userInfo != null)
                    {
                        if (userInfo.Passwd == userPwd)
                        {
                            if (userInfo.IsActive == "Y")
                            {
                                if (LoginServiceClientProxy.GetInstance().CheckIsVendorExist(userName, "VENDOR") == "Y")
                                {
                                    userInfo.Preference = this.PreferenceSession(userInfo.UserID);
                                    base.UserInfo = userInfo;
                                    this.WriteLoginLog(userInfo.UserID, userInfo.UserName, "OK");
                                    this.Session.Remove("ValidateCode");
                                    this.Session["LoginPageFrom"] = "portal";
                                    base.Server.Transfer("Frame_Main.aspx?from=Portal", false);
                                }
                                else
                                {
                                    this.lblMsg.Text = "该用户不是供应商,无法登录!";
                                }
                            }
                            else
                            {
                                this.lblMsg.Text = "该用户已经被停用无法登陆!";
                            }
                        }
                        else
                        {
                            this.WriteLoginLog(userName, userName, "Failed");
                            this.lblMsg.Text = "用户名或密码不正确!";
                        }
                    }
                    else
                    {
                        this.WriteLoginLog(userName, userName, "Failed");
                        this.lblMsg.Text = "用户名或密码不正确!";
                    }
                }
            }
            catch (Exception ex)
            {
                SysLog.GetInstance().LogError(ex, "用户登录异常!");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Page.Title = base.GetValueFromAppSettings("PAGE_PORTAL_TITLE");
            this.lblUserName.Text = base.GetValueFromAppSettings("LOGIN_USER_NAME");
            this.lblPassword.Text = base.GetValueFromAppSettings("LOGIN_PASSWORD");
            this.btnLogin.Text = base.GetValueFromAppSettings("LOGIN_BUTTON");
            string strimgUrl = base.GetValueFromAppSettings("LOGIN_IMAGE_URL");
            if (!File.Exists(base.Server.MapPath(strimgUrl)))
            {
                strimgUrl = "~/Images/Clientlogo.gif";
            }
            this.imgLogin.Src = strimgUrl;
            this.Session.Clear();
            if (!(string.IsNullOrEmpty(base.Request.QueryString["from"]) || !(base.Request.QueryString["from"].ToString() == "Logout")))
            {
                HttpContext.Current.Response.Write("<script language=javascript>top.location.href='Portal.aspx'</script>");
                HttpContext.Current.Response.Write("<script language=javascript>window.location.href='Portal.aspx'</script>");
                HttpContext.Current.Response.Write("<script language=javascript>window.parent.close()</script>");
            }
        }

        private PreferenceInfo PreferenceSession(string UserID)
        {
            PreferenceInfo Preference = new PreferenceInfo();
            PreferenceProfileInfo preferencePrifileInfo = LoginServiceClientProxy.GetInstance().GetPreferenceProfileByUserID(UserID);
            for (int i = 0; preferencePrifileInfo.PreferenceProfileDetailInfos.Count > i; i++)
            {
                switch (preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceID.ToUpper())
                {
                    case "COUNTRY":
                        Preference.Country = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;

                    case "CURRENCY":
                        Preference.Currency = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;

                    case "DATE_FORMAT":
                        Preference.DateFormat = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;

                    case "DIGITS_NO":
                        Preference.Digits_No = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;

                    case "LANG":
                        Preference.Lang = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;

                    case "TIME_FORMAT":
                        Preference.Time_Format = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;

                    case "TIMEZONE":
                        Preference.TimeZone = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;

                    case "THEMES":
                        Preference.Themes = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;

                    case "TIME":
                        Preference.Time = preferencePrifileInfo.PreferenceProfileDetailInfos[i].PreferenceValue;
                        break;
                }
            }
            return Preference;
        }

        private void WriteLoginLog(string userID, string userName, string loginStatus)
        {
            SysLoginLogInfo sysLoginLogInfo = new SysLoginLogInfo {
                LoginUserID = userID,
                LoginUserName = userName,
                LoginStatus = loginStatus,
                LoginMode = "MainSystem",
                LoginTime = DateTime.Now,
                LoginIpAddress = base.Request.UserHostAddress,
                LoginMachineName = base.Request.UserHostName,
                LoginMacAddress = "",
                CreatedBy = userID
            };
            LoginServiceClientProxy.GetInstance().WriteLoginLog(sysLoginLogInfo);
        }
    }
}

